//-------------------------------------------------------------------------
// File: RegistryKey.cpp
//
// Descr.: - todo -
//
// Author: Stefan Leihkauf
// Date:   8/8/2011
//-------------------------------------------------------------------------
#include "cb_pch.h"
#include "registry/RegistryKey.h"
#include "registry/RegistryValue.h"

unsigned const RegistryKey::MaxKeyName	= 255;
unsigned const RegistryKey::MaxValueName	= 16383;

//-------------------------------------------------------------------------
/**
*/
RegistryKey::RegistryKey(WinHKey handle, const StdString& name) :
_name(name),
_hkey(handle)
{
	EnumSubkeyNames();
	EnumKeyValues();
}

//-------------------------------------------------------------------------
/**
*/
RegistryKey::~RegistryKey()
{
}

//-------------------------------------------------------------------------
/**
*/
const StdString& RegistryKey::Name() const
{
	return _name;
}

//-------------------------------------------------------------------------
/**
*/
const RegistryKey::KeyValueList& RegistryKey::Values() const
{
	return _keyvalues;
}

//-------------------------------------------------------------------------
/**
*/
const RegistryKey::SubKeyNameList& RegistryKey::SubKeyNames() const
{
	return _subkeynames;
}

//-------------------------------------------------------------------------
/**
*/
void RegistryKey::Close()
{
	_hkey.reset();
}

//-------------------------------------------------------------------------
/**
*/
RegistryKey RegistryKey::OpenSubKey(const StdString& name, const RegistryKey::AccessRights accessRights /*= Read*/) const
{
	HKEY handle = 0;
	if(ERROR_SUCCESS == RegOpenKeyEx((HKEY)_hkey.get(), name.c_str(), 0, accessRights.to_ulong(), &handle))
		return RegistryKey(handle, name);

	return RegistryKey(handle, name);
}

//-------------------------------------------------------------------------
/**
*/
RegistryKey RegistryKey::CreateSubKey(const StdString& name, const RegistryKey::AccessRights accessRights /*= Write*/)
{
	HKEY handle = 0;

	if(ERROR_SUCCESS == RegCreateKeyEx((HKEY)_hkey.get(), name.c_str(), 0, 0, 0, accessRights.to_ulong(), 0, &handle, 0)) {
		//EnumSubkeys();
		return RegistryKey(handle, name);
	}

	return RegistryKey(handle, name);
}

//-------------------------------------------------------------------------
/**
*/
void RegistryKey::EnumSubkeyNames()
{
	TCHAR buffer[RegistryKey::MaxKeyName];
	unsigned long szBuffer = RegistryKey::MaxKeyName;
	unsigned long nKey = 0;

	_subkeynames.clear();
	while(ERROR_SUCCESS == RegEnumKeyEx(_hkey, nKey, buffer, &szBuffer, 0, 0, 0, 0)) {
		_subkeynames.push_back(buffer);
		szBuffer = RegistryKey::MaxKeyName;
		nKey++;
	}
}

//-------------------------------------------------------------------------
/**
*/
void RegistryKey::EnumKeyValues()
{
	TCHAR valueName[RegistryKey::MaxValueName];
	unsigned long szValueName = RegistryKey::MaxValueName;
	unsigned long nKey = 0;

	_keyvalues.clear();
	while(ERROR_SUCCESS == RegEnumValue(_hkey, nKey, valueName, &szValueName, 0, 0, 0, 0)) {
		_keyvalues.push_back(RegistryValue(_hkey, valueName));
		szValueName = RegistryKey::MaxValueName;
		nKey++;
	}
}